package cn.edu.scau.cmi.hombio.service;

import cn.edu.scau.cmi.hombio.dao.TaskDAO;
import cn.edu.scau.cmi.hombio.dao.TaskStatusTypeDAO;

import cn.edu.scau.cmi.hombio.domain.Task;
import cn.edu.scau.cmi.hombio.domain.TaskStatusType;

import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

/**
 * Spring service that handles CRUD requests for TaskStatusType entities
 * 
 */

@Service("TaskStatusTypeService")

@Transactional
public class TaskStatusTypeServiceImpl implements TaskStatusTypeService {

	/**
	 * DAO injected by Spring that manages Task entities
	 * 
	 */
	@Autowired
	private TaskDAO taskDAO;

	/**
	 * DAO injected by Spring that manages TaskStatusType entities
	 * 
	 */
	@Autowired
	private TaskStatusTypeDAO taskStatusTypeDAO;

	/**
	 * Instantiates a new TaskStatusTypeServiceImpl.
	 *
	 */
	public TaskStatusTypeServiceImpl() {
	}

	/**
	 */
	@Transactional
	public TaskStatusType findTaskStatusTypeByPrimaryKey(Integer id) {
		return taskStatusTypeDAO.findTaskStatusTypeByPrimaryKey(id);
	}

	/**
	 * Return all TaskStatusType entity
	 * 
	 */
	@Transactional
	public List<TaskStatusType> findAllTaskStatusTypes(Integer startResult, Integer maxRows) {
		return new java.util.ArrayList<TaskStatusType>(taskStatusTypeDAO.findAllTaskStatusTypes(startResult, maxRows));
	}

	/**
	 * Delete an existing TaskStatusType entity
	 * 
	 */
	@Transactional
	public void deleteTaskStatusType(TaskStatusType taskstatustype) {
		taskStatusTypeDAO.remove(taskstatustype);
		taskStatusTypeDAO.flush();
	}

	/**
	 * Return a count of all TaskStatusType entity
	 * 
	 */
	@Transactional
	public Integer countTaskStatusTypes() {
		return ((Long) taskStatusTypeDAO.createQuerySingleResult("select count(o) from TaskStatusType o").getSingleResult()).intValue();
	}

	/**
	 * Save an existing TaskStatusType entity
	 * 
	 */
	@Transactional
	public void saveTaskStatusType(TaskStatusType taskstatustype) {
		TaskStatusType existingTaskStatusType = taskStatusTypeDAO.findTaskStatusTypeByPrimaryKey(taskstatustype.getId());

		if (existingTaskStatusType != null) {
			if (existingTaskStatusType != taskstatustype) {
				existingTaskStatusType.setId(taskstatustype.getId());
				existingTaskStatusType.setName(taskstatustype.getName());
				existingTaskStatusType.setMemo(taskstatustype.getMemo());
			}
			taskstatustype = taskStatusTypeDAO.store(existingTaskStatusType);
		} else {
			taskstatustype = taskStatusTypeDAO.store(taskstatustype);
		}
		taskStatusTypeDAO.flush();
	}

	/**
	 * Load an existing TaskStatusType entity
	 * 
	 */
	@Transactional
	public Set<TaskStatusType> loadTaskStatusTypes() {
		return taskStatusTypeDAO.findAllTaskStatusTypes();
	}

	/**
	 * Delete an existing Task entity
	 * 
	 */
	@Transactional
	public TaskStatusType deleteTaskStatusTypeTasks(Integer taskstatustype_id, Integer related_tasks_id) {
		Task related_tasks = taskDAO.findTaskByPrimaryKey(related_tasks_id, -1, -1);

		TaskStatusType taskstatustype = taskStatusTypeDAO.findTaskStatusTypeByPrimaryKey(taskstatustype_id, -1, -1);

		related_tasks.setTaskStatusType(null);
		taskstatustype.getTasks().remove(related_tasks);

		taskDAO.remove(related_tasks);
		taskDAO.flush();

		return taskstatustype;
	}

	/**
	 * Save an existing Task entity
	 * 
	 */
	@Transactional
	public TaskStatusType saveTaskStatusTypeTasks(Integer id, Task related_tasks) {
		TaskStatusType taskstatustype = taskStatusTypeDAO.findTaskStatusTypeByPrimaryKey(id, -1, -1);
		Task existingtasks = taskDAO.findTaskByPrimaryKey(related_tasks.getId());

		// copy into the existing record to preserve existing relationships
		if (existingtasks != null) {
			existingtasks.setId(related_tasks.getId());
			existingtasks.setTaskNumber(related_tasks.getTaskNumber());
			existingtasks.setCreateTime(related_tasks.getCreateTime());
			existingtasks.setStartTime(related_tasks.getStartTime());
			existingtasks.setEndTime(related_tasks.getEndTime());
			existingtasks.setMemo(related_tasks.getMemo());
			related_tasks = existingtasks;
		}

		related_tasks.setTaskStatusType(taskstatustype);
		taskstatustype.getTasks().add(related_tasks);
		related_tasks = taskDAO.store(related_tasks);
		taskDAO.flush();

		taskstatustype = taskStatusTypeDAO.store(taskstatustype);
		taskStatusTypeDAO.flush();

		return taskstatustype;
	}
}
